package com.topscomm.core.util;

import org.apache.commons.codec.binary.Base64;

/**
 * token格式化工具 格式：header(可以没有,明文).data.sign
 * 
 * @description:
 * @author: donghang
 * @date: 2021年3月13日下午11:29:51
 * @modify:
 */
public class TokenForamtUtil {
	public static final String TOKEN_SPLIT = ".";

	// 位置标记
	public static final int DATA_INDEX = 0, SING_INDEX = 1;

	public static String format(String data, String sign) {
		return data + TOKEN_SPLIT + sign;
	}

	public static String format(byte[] data, byte[] sign) {
		return format(Base64.encodeBase64URLSafeString(data), Base64.encodeBase64URLSafeString(sign));
	}

	public static String[] splitToken(String token) {
		return token.split("\\" + TOKEN_SPLIT);
	}

	public static byte[][] splitTokenToByte(String token) {
		String[] split = splitToken(token);
		byte[][] bytes = new byte[split.length][];
		for (int i = 0; i < split.length; i++) {
			bytes[i] = Base64.decodeBase64(split[i]);
		}
		return bytes;
	}

}
